package org.jeecg.modules.wlhy.setting.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.modules.wlhy.setting.entity.HySetting;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: hy_setting
 * @Author: jeecg-boot
 * @Date:   2021-04-23
 * @Version: V1.0
 */
@Api(tags="hy_setting")
@RestController
@RequestMapping("/setting/hySetting")
@Slf4j
public class HySettingController extends JeecgController<HySetting, IHySettingService> {
	@Autowired
	private IHySettingService hySettingService;

	 //获取设置列表
//	 @AutoLog(value = "hy_setting-分页列表查询")
	 @ApiOperation(value="hy_setting-分页列表查询", notes="hy_setting-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> list() {
//		 Page<HySetting> page = new Page<HySetting>(1, 50);
//		 IPage<HySetting> pageList = hySettingService.page(page);
		 return Result.OK(hySettingService.list());
	 }
	/**
	 *   添加
	 *
	 * @param hySetting
	 * @return
	 */
	@AutoLog(value = "hy_setting-添加")
	@ApiOperation(value="hy_setting-添加", notes="hy_setting-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HySetting hySetting) {
		hySettingService.save(hySetting);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hySetting
	 * @return
	 */
	@AutoLog(value = "hy_setting-编辑")
	@ApiOperation(value="hy_setting-编辑", notes="hy_setting-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HySetting hySetting) {
		hySettingService.edit(hySetting);
		hySettingService.clearCache();
		return Result.OK("编辑成功!");
	}

	 @AutoLog(value = "hy_setting-编辑密码")
	 @ApiOperation(value="hy_setting-编辑密码", notes="hy_setting-编辑密码")
	 @PutMapping(value = "/editpwd")
	 public Result<?> editpwd(@RequestBody JSONObject jsonObject) {
		 //hySettingService.edit(hySetting);
		 String id = jsonObject.getString("id");
		 HySetting entity = hySettingService.getById(id);
		 if(entity==null){
			 return Result.error("数据不存在！");
		 }
		 String oldPwd = jsonObject.getString("oldpassword");
		 String password = jsonObject.getString("password");
		 if(StringUtils.isEmpty(oldPwd)){
			 return Result.error("原始密码不能为空！");
		 }
		 if(StringUtils.isEmpty(password)){
			 return Result.error("新密码不能为空！");
		 }
		 String encryptPayPwd = PasswordUtil.encrypt("pd", oldPwd,  PasswordUtil.BSalt);
		 if(!StringUtils.equals(encryptPayPwd,entity.getData())){
			 return Result.error("原始密码错误！");
		 }
		 String newEncryptPayPwd = PasswordUtil.encrypt("pd", password,  PasswordUtil.BSalt);
		 entity.setData(newEncryptPayPwd);

		 hySettingService.edit(entity);
		 hySettingService.clearCache();
		 return Result.OK("编辑成功!");
	 }

//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_setting-通过id删除")
//	@ApiOperation(value="hy_setting-通过id删除", notes="hy_setting-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		hySettingService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "hy_setting-批量删除")
//	@ApiOperation(value="hy_setting-批量删除", notes="hy_setting-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hySettingService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_setting-通过id查询", notes="hy_setting-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HySetting hySetting = hySettingService.getById(id);
		if(hySetting==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hySetting);
	}

	 /**
	  * 通过tag查询
	  *
	  * @param tag
	  * @return
	  */
	 @ApiOperation(value="hy_setting-通过tag查询", notes="hy_setting-通过tag查询")
	 @GetMapping(value = "/queryByTag")
	 public Result<?> queryByTag(@RequestParam(name="tag",required=true) String tag) {
		 String settingValue = hySettingService.getSettingValueByTag(tag);
		 return Result.OK(settingValue);
	 }

	 /**
	  * 查询网站footer所需信息
	  *
	  * @return
	  */
	 @ApiOperation(value="hy_setting-查询网站footer所需信息", notes="hy_setting-查询网站footer所需信息")
	 @GetMapping(value = "/queryFooterSettings")
	 public Result<?> queryFooterSettings() {
		 Map<String, String> settings = new HashMap<>();
		 settings.put("footer_address", hySettingService.getSettingValueByTag("footer_address"));
		 settings.put("footer_customer_service", hySettingService.getSettingValueByTag("footer_customer_service"));
		 settings.put("footer_company_name", hySettingService.getSettingValueByTag("footer_company_name"));
		 settings.put("footer_company_name_link", hySettingService.getSettingValueByTag("footer_company_name_link"));
		 settings.put("footer_web_record_number", hySettingService.getSettingValueByTag("footer_web_record_number"));
		 settings.put("footer_ICP_number", hySettingService.getSettingValueByTag("footer_ICP_number"));

		 settings.put("platform_logo_light", hySettingService.getSettingValueByTag("platform_logo_light"));
		 settings.put("platform_logo_dark", hySettingService.getSettingValueByTag("platform_logo_dark"));
		 settings.put("web_platform_name", hySettingService.getSettingValueByTag("web_platform_name"));
		 settings.put("logo_title", hySettingService.getSettingValueByTag("logo_title"));
		 settings.put("footer_jiancheng_name", hySettingService.getSettingValueByTag("footer_jiancheng_name"));

		 settings.put("vehicle_track_switch", hySettingService.getSettingValueByTag("vehicle_track_switch"));
		 settings.put("contract_template", hySettingService.getSettingValueByTag("contract_template"));

		 return Result.OK(settings);
	 }

	 /**
	  * 查询平台设置(平台名称、LOGO等)
	  *
	  * @return
	  */
	 @ApiOperation(value="hy_setting-查询平台设置", notes="hy_setting-查询平台设置(平台名称、LOGO等)")
	 @GetMapping(value = "/queryPlatformSettings")
	 public Result<?> queryPlatformSettings() {
		 Map<String, String> settings = new HashMap<>();
		 settings.put("footer_address", hySettingService.getSettingValueByTag("footer_address"));
		 settings.put("footer_customer_service", hySettingService.getSettingValueByTag("footer_customer_service"));
		 settings.put("footer_company_name", hySettingService.getSettingValueByTag("footer_company_name"));
		 settings.put("footer_company_name_link", hySettingService.getSettingValueByTag("footer_company_name_link"));
		 settings.put("footer_web_record_number", hySettingService.getSettingValueByTag("footer_web_record_number"));
		 settings.put("footer_ICP_number", hySettingService.getSettingValueByTag("footer_ICP_number"));

		 settings.put("platform_logo_light", hySettingService.getSettingValueByTag("platform_logo_light"));
		 settings.put("platform_logo_dark", hySettingService.getSettingValueByTag("platform_logo_dark"));
		 settings.put("web_platform_name", hySettingService.getSettingValueByTag("web_platform_name"));
		 settings.put("logo_title", hySettingService.getSettingValueByTag("logo_title"));
		 settings.put("footer_jiancheng_name", hySettingService.getSettingValueByTag("footer_jiancheng_name"));

		 settings.put("vehicle_track_switch", hySettingService.getSettingValueByTag("vehicle_track_switch"));
		 settings.put("bg", hySettingService.getSettingValueByTag("bg"));
		 settings.put("contract_template", hySettingService.getSettingValueByTag("contract_template"));

		 settings.put("web_introduce", hySettingService.getSettingValueByTag("web_introduce"));
		 return Result.OK(settings);
	 }






}
